'use strict';
// 申请开通视图模块
import {
    titileInputInit
} from '../components/titleInput.js';


var $iosActionsheet, $iosMask, $citiesComponent, $cityBox, $provinceBox, $verifyBtn;
var cityStat = false;
var _provinceName = "",
    _cityName = "";
var locProv, locCity;
var daojs = 60;
var vparaments = {
    "locProv": "",
    "locCity": "",
    "provinceName": "",
    "cityName": "",
    "provinceAndCityName": ""
}

var $qyzch, $name, $phone, $road, $areaName, $applyBtn;

// 视图初始化
function viewInit() {
    initValue();
    titileInputInit();
    initSheet();
    initCitiesComponet();
    $verifyBtn.on("click", smsSend); //发送短信
    $("#phone").on("input", checkVerify);
    $("#fanhui").on("touchstart", function() {
        location.href = LoginInit_URL;
    });
    
}

// 判断是否允许发送验证码
function checkVerify() {
    var lodinVal = $("#phone").val();

    if (lodinVal.length == 11 && daojs == 60) {
        $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
    } else {
        $verifyBtn.attr("class", "weui-btn weui-btn_plain-default weui-btn_plain-disabled");
    }
}

// 取发送短信参数
function getsmsParameters() {
    var parameters = {};
    parameters.lodinId = $("#phone").val();
    parameters.sendType = "2"; //发送类型
    //获取url中的openid
    parameters.openid = openid;
    return parameters;
}


// 短信发送
function smsSend() {
    if ($verifyBtn.hasClass("weui-btn_plain-disabled")) {
        return;
    } else {
        djsfunc();
        var parameters = getsmsParameters();
        $.post(duanxin_URL, parameters, smsCallBack);
    }
}

// 验证码倒计时
function djsfunc() {
    var handle = setInterval(function() {
        $verifyBtn.text("重新获取(" + daojs + "S)");
        $verifyBtn.attr("class", "weui-btn weui-btn_plain-default weui-btn_plain-disabled");
        daojs--;
        if (daojs == 0) {
            clearInterval(handle);
            $verifyBtn.text("重新获取验证码");
            daojs = 60;
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        }
    }, 1000);
}

//短信发送 回调函数
function smsCallBack(msg) {
    console.log("msg");
    console.dir(msg);
    if (msg.code != "00") {
        errorObject.showErroDialog(msg.msg);
    }

}


/* city 省市到时提炼成组件
-----------------------------------------------------*/

// 返回城市选择参数
function getCityparaments() {
    return vparaments;
}
// 初始化省市事件
function initCitiesComponet() {
    var $cityPop = $('.city_pop');
    $('#cities-componet .reset_btn').on("touchend", function() {
        setTimeout(function() {
            $citiesComponent.fadeOut();
            $cityPop.css("bottom", -320);
        }, 200);
    });
    $('#cities-componet .confirm_btn').on("touchend", function() {
        setTimeout(function() {

            if (locCity == undefined || locCity == "") {
                $("#publicErrorDialog").css({
                    "opacity": "1",
                    "display": ""
                });

                $("#publicErrorDialog .weui-dialog__bd").text("请选择城市");
            } else {
                $citiesComponent.fadeOut();
                $cityPop.css("bottom", -320);
                $("#area_name").text(_provinceName + "/" + _cityName);
                vparaments.provinceAndCityName = _provinceName + _cityName;
                vparaments.locProv = locProv;
                vparaments.locCity = locCity;
                vparaments.provinceName = _provinceName;
                vparaments.cityName = _cityName;
                window.province = locCity;
            }

        }, 200);
    });
}

function selectCity(provinceAndCityArray) {
    createsheLi(provinceAndCityArray);
    var provinceList = $provinceBox.find("li").click(function() {
        var $this = $(this);
        var idx = provinceList.index(this);
        var cityListArray = provinceAndCityArray[idx].cities;
        locProv = $this.attr("id");
        _cityName = "";
        locCity = "";
        _provinceName = $this.text();
        $this.addClass('selected').siblings().removeClass('selected');
        setTimeout(function() {
            _createCityLi(cityListArray);

            _citySelect(this);
        }, 300);
    });


}

// 添加省li
function createsheLi(provinceAndCityArray) {
    var allProvinceLi = '';
    for (var i = 0; i < provinceAndCityArray.length; i++) {
        var provinceName = provinceAndCityArray[i].name;
        var provinceId = provinceAndCityArray[i].id;
        var provinceLi = "<li id=\"" + provinceId + "\">" + provinceName + "</li>";
        allProvinceLi += provinceLi;
        //provinceBox.find("li").eq(0).addClass("selected");
        //console.log(provinceName)
    }

    $provinceBox.html(allProvinceLi);
}

// 添加市li
function _createCityLi(cityListArray) {
    $cityBox.html(""); // 清空城市列表
    var allCityLi = '';
    var i = 0;

    for (; i < cityListArray.length; i++) {
        var _tempName = cityListArray[i].split("-");
        var cityId = _tempName[0];
        var cityName = _tempName[1];
        var cityLi = "<li id=\"" + cityId + "\">" + cityName + "</li>";
        allCityLi += cityLi;

    }
    $cityBox.html(allCityLi);

}

function _citySelect() {
    $cityBox.find("li").click(function() {
        _cityName = $(this).text();
        locCity = $(this).attr("id");
        $(this).addClass('selected').siblings().removeClass('selected');
        cityStat = true;
    })
}

/* city 省市到时提炼成组件
-----------------------------------------------------*/


// 判断所有input是否都填
function checkedInput() {
    var checkVal = "false";
    if ($qyzch.val() == "") {
        checkVal = "qyzch-no";
    } else if ($name.val() == "") {
        checkVal = "name-no";
    } else if ($phone.val() == "") {
        checkVal = "phone-no";
    } else if ($road.val() == "") {
        checkVal = "road-no";
    } else if (!window.province) {
        checkVal = "areaName-no";
    } else {
        checkVal = "ok"
    }

    return checkVal;

}

// 显示loading
function showLoadingDialog() {
    $("#loadingDialog").css({
        "opacity": "1",
        "display": ""
    });
}

// 隐藏loading
function hideLoadingDialog() {
    $("#loadingDialog").fadeOut(200);
}

// 初始化变量
function initValue() {
    $iosActionsheet = $('#iosActionsheet');
    $iosMask = $('#iosMask');
    $citiesComponent = $('#cities-componet');
    $provinceBox = $(".province_list ul");
    $cityBox = $('.city_list ul');
    $verifyBtn = $("#verifyBtn");
    $qyzch = $("#qyzch");
    $name = $("#name");
    $phone = $("#phone");
    $road = $("#road");
    $areaName = $("#area_name");
    $applyBtn = $("#apply_btn");
}

// 隐藏地区弹出框
function hideActionSheet() {
    $iosActionsheet.removeClass('weui-actionsheet_toggle');
    $iosMask.fadeOut(200);
}

// 初始化 地区弹出框事件
function initSheet() {
    $('#iosActionsheetCancel').on('click', hideActionSheet);
    $("#addressBtn").on("click", function() {
        $iosActionsheet.addClass('weui-actionsheet_toggle');
        $iosMask.fadeIn(200);
    });
}



export {
    viewInit,
    selectCity,
    getCityparaments,
    checkedInput,
    showLoadingDialog,
    hideLoadingDialog
}